/*
 * mostrarReceta.java
 *
 * Created on 25 de octubre de 2008, 16:30
 */

package clases;

import clases.*;
import java.awt.Component;
import java.awt.Image;
import java.awt.Point;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.Persistence;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  salader
 */
public class mostrarReceta extends javax.swing.JFrame {
    //Paso que se esta mostrando actualmente
    private int step = 0;
    //Numero total de pasos en al receta
    private int stepT = 0;
    //Indice de la descripcion/foto de la receta seleccionada
    private int desc = 0;
    //Indicador de si ya ha sido cargada y mostrada la lista o no
    private boolean cargado = false;
    //Vector con todas las recetas que se van a mostrar
    private Vector<Receta> recetas = new Vector<Receta>(0,1);
    //Receta seleccionada de la lista, actualmente ninguna
    private int sel = -1;
    //Receta actualmente marcada
    private Receta rec = null;
    public void setRecetas(Vector<Receta> r2){
        recetas = r2;
        DefaultTableModel modelo = new DefaultTableModel();
        modelo.addColumn("Nombre");
        modelo.addColumn("Dificultad");
        modelo.addColumn("Tipo");
        modelo.addColumn("Nacionalidad");
    
        Receta r = new Receta();
        int i = 0;
    
        while(i < recetas.size()){
            r = recetas.get(i);
            modelo.insertRow(i, new Object[]{recetas.elementAt(i).getNombre(),recetas.elementAt(i).getDificultad(),
                 recetas.elementAt(i).getTipo().getNombre(),recetas.elementAt(i).getNacionalidad().getNombre()});
            i++;
        }
        seleccion.setModel(modelo);
    }
    public void crearListaRecetas(){
        /*Se obtiene la lista de recetas de la BBDD*/
        /*Para el ejemplo emplearemos dos recetas creadas directamente desde
         el metodo*/
        /*
        Nacionalidad n1 = new Nacionalidad(1); n1.setNombre("España");
        Nacionalidad n2 = new Nacionalidad(2); n2.setNombre("Francia");
        Tipo t1 = new Tipo(1); t1.setNombre("Carnes");
        Tipo t2 = new Tipo(2); t2.setNombre("Carnes");
        Bebida b1 = new Bebida(1); b1.setNombre("Agua"); b1.setNacionalidad(n1);
        Bebida b2 = new Bebida(2); b1.setNombre("Champagne"); b1.setNacionalidad(n2);
        
        Receta r1 = new Receta(1);
        r1.setRef("1"); r1.setNombre("Huevos Fritos");
        r1.setDificultad("Media"); r1.setTipo(t1);
        r1.setNacionalidad(n1); r1.setDuracion(15);
        
        Receta r2 = new Receta(2); 
        r2.setRef("2"); r2.setNombre("Tortilla Francesa");
        r2.setDificultad("baja"); r2.setTipo(t2);
        r2.setNacionalidad(n2); r2.setDuracion(10);
        
        recetas.add(r1);
        recetas.add(r2);
        */
        recetas = cargarBD();
    }
    /** Creates new form mostrarReceta */
    public mostrarReceta() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pasoApaso = new javax.swing.JFrame();
        Siguiente = new javax.swing.JButton();
        Anterior = new javax.swing.JButton();
        descripcionPaso = new javax.swing.JScrollPane();
        Pasos = new javax.swing.JTextArea();
        Imagen = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        Paso = new javax.swing.JLabel();
        mostrarDetalles = new javax.swing.JFrame();
        dificultadDes = new javax.swing.JLabel();
        duracionDes = new javax.swing.JLabel();
        kcalDes = new javax.swing.JLabel();
        tipoDes = new javax.swing.JLabel();
        nacionalidadDes = new javax.swing.JLabel();
        nombreDes = new javax.swing.JLabel();
        dificultadDes2 = new javax.swing.JLabel();
        duracionDes2 = new javax.swing.JLabel();
        kcalDes2 = new javax.swing.JLabel();
        tipoDes2 = new javax.swing.JLabel();
        nacionalidadDes2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        ingredientes = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        descripcionDes = new javax.swing.JTextArea();
        fotoDes = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        seleccion = new javax.swing.JTable();
        foto = new javax.swing.JLabel();
        paso = new javax.swing.JButton();
        NombreLabel = new javax.swing.JLabel();
        DificultadLabel = new javax.swing.JLabel();
        TipoLabel = new javax.swing.JLabel();
        NacionalidadLabel = new javax.swing.JLabel();
        KiloLabel = new javax.swing.JLabel();
        VinoLabel = new javax.swing.JLabel();
        DuracionLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        localizar = new javax.swing.JButton();
        cargar = new javax.swing.JButton();
        Detalles = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        Menu16 = new javax.swing.JMenuBar();
        Archivo16 = new javax.swing.JMenu();
        Imprimir16 = new javax.swing.JMenuItem();

        pasoApaso.setTitle("Pasos");
        pasoApaso.setMinimumSize(new java.awt.Dimension(520, 300));
        pasoApaso.setResizable(false);

        Siguiente.setText("Siguiente");
        Siguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SiguienteActionPerformed(evt);
            }
        });

        Anterior.setText("Anterior");
        Anterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AnteriorActionPerformed(evt);
            }
        });

        Pasos.setColumns(20);
        Pasos.setRows(5);
        descripcionPaso.setViewportView(Pasos);

        jLabel7.setText("PASO: ");

        javax.swing.GroupLayout pasoApasoLayout = new javax.swing.GroupLayout(pasoApaso.getContentPane());
        pasoApaso.getContentPane().setLayout(pasoApasoLayout);
        pasoApasoLayout.setHorizontalGroup(
            pasoApasoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pasoApasoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pasoApasoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(descripcionPaso, javax.swing.GroupLayout.PREFERRED_SIZE, 403, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pasoApasoLayout.createSequentialGroup()
                        .addGroup(pasoApasoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(pasoApasoLayout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(Paso))
                            .addComponent(Anterior, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(193, 193, 193)
                        .addGroup(pasoApasoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(Imagen, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                            .addComponent(Siguiente))))
                .addContainerGap())
        );
        pasoApasoLayout.setVerticalGroup(
            pasoApasoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pasoApasoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Imagen)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(descripcionPaso, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pasoApasoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(Paso))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pasoApasoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Anterior)
                    .addComponent(Siguiente))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        mostrarDetalles.setMinimumSize(new java.awt.Dimension(600, 400));

        dificultadDes.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        dificultadDes.setText("Dificultad:");

        duracionDes.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        duracionDes.setText("Duracion:");

        kcalDes.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        kcalDes.setText("Energía: ");

        tipoDes.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        tipoDes.setText("Tipo");

        nacionalidadDes.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        nacionalidadDes.setText("Nacionalidad:");

        nombreDes.setFont(new java.awt.Font("Comic Sans MS", 3, 18));
        nombreDes.setText("NOMBRE DE LA RECETA");

        dificultadDes2.setText("dificultad");

        duracionDes2.setText("duracion");

        kcalDes2.setText("KCals");

        tipoDes2.setText("tipo");

        nacionalidadDes2.setText("nacionalidad");

        ingredientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Producto", "Cantidad"
            }
        ));
        jScrollPane2.setViewportView(ingredientes);

        descripcionDes.setColumns(20);
        descripcionDes.setRows(5);
        jScrollPane3.setViewportView(descripcionDes);

        javax.swing.GroupLayout mostrarDetallesLayout = new javax.swing.GroupLayout(mostrarDetalles.getContentPane());
        mostrarDetalles.getContentPane().setLayout(mostrarDetallesLayout);
        mostrarDetallesLayout.setHorizontalGroup(
            mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mostrarDetallesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mostrarDetallesLayout.createSequentialGroup()
                        .addComponent(dificultadDes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dificultadDes2))
                    .addGroup(mostrarDetallesLayout.createSequentialGroup()
                        .addComponent(duracionDes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(duracionDes2))
                    .addGroup(mostrarDetallesLayout.createSequentialGroup()
                        .addComponent(tipoDes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tipoDes2))
                    .addGroup(mostrarDetallesLayout.createSequentialGroup()
                        .addGroup(mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mostrarDetallesLayout.createSequentialGroup()
                                .addComponent(kcalDes)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(kcalDes2))
                            .addComponent(nacionalidadDes, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nacionalidadDes2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nombreDes)
                    .addComponent(fotoDes, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE))
                .addContainerGap())
        );
        mostrarDetallesLayout.setVerticalGroup(
            mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mostrarDetallesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nombreDes)
                .addGap(36, 36, 36)
                .addGroup(mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(fotoDes, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mostrarDetallesLayout.createSequentialGroup()
                        .addGroup(mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dificultadDes)
                            .addComponent(dificultadDes2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(duracionDes)
                            .addComponent(duracionDes2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(kcalDes)
                            .addComponent(kcalDes2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tipoDes)
                            .addComponent(tipoDes2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nacionalidadDes)
                            .addComponent(nacionalidadDes2))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mostrarDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE))
                .addContainerGap())
        );

        setTitle("Lista de recetas");
        setBackground(new java.awt.Color(51, 51, 51));
        setForeground(java.awt.Color.black);
        setName("mostrarReceta"); // NOI18N

        seleccion.setBackground(new java.awt.Color(254, 255, 255));
        seleccion.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        seleccion.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Dificultad", "Tipo", "Nacionalidad"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        seleccion.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                seleccionMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(seleccion);
        seleccion.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        paso.setText("Pasos");
        paso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pasoActionPerformed(evt);
            }
        });

        NombreLabel.setFont(new java.awt.Font("Comic Sans MS", 3, 18));
        NombreLabel.setText("Nombre de la Receta");

        DificultadLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        DificultadLabel.setText("Dificultad");

        TipoLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        TipoLabel.setText("Tipo de cominda");

        NacionalidadLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        NacionalidadLabel.setText("Nacionalidad");

        KiloLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        KiloLabel.setText("KiloCalorias");

        VinoLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        VinoLabel.setText("Vino");

        DuracionLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 12));
        DuracionLabel.setText("Duración");

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        jLabel1.setText("Dificultad:");

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        jLabel2.setText("Duración:");

        jLabel3.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        jLabel3.setText("Energía:");

        jLabel4.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        jLabel4.setText("Tipo:");

        jLabel5.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        jLabel5.setText("Nacionalidad:");

        jLabel6.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        jLabel6.setText("Vino Recomendado:");

        localizar.setText("Localizar");
        localizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                localizarActionPerformed(evt);
            }
        });

        cargar.setText("Cargar");
        cargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cargarActionPerformed(evt);
            }
        });

        Detalles.setText("Detalles");
        Detalles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DetallesActionPerformed(evt);
            }
        });

        jButton1.setText("Combinar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Restaur.");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Compra");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        Archivo16.setText("Archivo");
        Archivo16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ArchivoActionPerformed(evt);
            }
        });

        Imprimir16.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        Imprimir16.setText("Imprimir");
        Imprimir16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ImprimirActionPerformed(evt);
            }
        });
        Archivo16.add(Imprimir16);

        Menu16.add(Archivo16);

        setJMenuBar(Menu16);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(NacionalidadLabel))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(DificultadLabel))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(DuracionLabel))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(KiloLabel))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(TipoLabel))
                                .addComponent(jLabel6)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addComponent(VinoLabel)))
                            .addGap(12, 12, 12)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(NombreLabel)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 213, Short.MAX_VALUE)
                                    .addComponent(localizar))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addComponent(foto, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(paso, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(Detalles))
                                        .addComponent(jButton1)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                            .addGap(0, 0, 0))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 632, Short.MAX_VALUE)
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cargar)
                        .addContainerGap())))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {Detalles, cargar, jButton1, jButton2, jButton3, localizar, paso});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NombreLabel)
                    .addComponent(localizar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(DificultadLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(DuracionLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(KiloLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel4)
                                    .addComponent(TipoLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel5)
                                    .addComponent(NacionalidadLabel))
                                .addGap(26, 26, 26)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(VinoLabel)
                                .addGap(17, 17, 17))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(paso)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Detalles)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3)))
                        .addGap(160, 160, 160)
                        .addComponent(cargar)
                        .addGap(6, 6, 6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(foto, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
public Receta buscarReceta(String n){
    Receta r = null;
    int i = 0;
    while(i < recetas.size()){
        if(recetas.get(i).getNombre() == n)return recetas.get(i);
        i++;
    }
    return r;
}
public String leerFichero(String n){
    String f = new String();
    String desc = "c:/is2/desc/" + n + ".txt";
    FileInputStream arc;
    int v;
    try {
        try {
            arc = new FileInputStream(desc);
            while ((v = arc.read()) != -1) {
            f += (char) v;
            }
            arc.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(mostrarReceta.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (IOException ex) {
        Logger.getLogger(mostrarReceta.class.getName()).log(Level.SEVERE, null, ex);
    }
    return f;
}
private void cargarImagen(String n, javax.swing.JLabel fotos){
    String dir = "c:/is2/fotos/"+n+"ico.png";
    ImageIcon icon = new ImageIcon(dir,"Foto de la Receta");
    fotos.setIcon(null);
    fotos.setIcon(icon);
}
private Vector<Receta> cargarBD(){
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("recetasUnit");
    EntityManager em;
    em = emf.createEntityManager();
    em.setFlushMode(FlushModeType.AUTO);
    Vector<Receta> recetas2 = new Vector<Receta>(0,1);
    recetas2 = (Vector<Receta>)em.createQuery("SELECT r FROM Receta r WHERE r.id>0").getResultList();
    Collections.sort(recetas2);
    return recetas2;
}
public void mostrarPasos(int i){
    step = 1;
    desc = i;
    stepT = Integer.parseInt(leerFichero(i+"/conf"));
    String fichero = leerFichero(i+"/"+step);
    Pasos.setText(fichero);
    cargarImagen(i+"/"+step,Imagen);
}
private void cargarAnterior(){
    String fichero = leerFichero(desc+"/"+step);
    Pasos.setText(fichero);
    cargarImagen(desc+"/"+step,Imagen);
}
private void SiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SiguienteActionPerformed
// TODO add your handling code here:
    if(step < stepT){//GEN-LAST:event_SiguienteActionPerformed
        step++;
        cargarAnterior();
        Paso.setText(""+step);
    }
    else System.out.println("Alcanzada la ultima hoja de descripcion.");
}                                         
private void AnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnteriorActionPerformed
// TODO add your handling code here:
    if(step != 1){
        step--;
        cargarAnterior();
        Paso.setText(""+step);
    }
    else System.out.println("Alcanzada la primera hoja de descripcion.");
}//GEN-LAST:event_AnteriorActionPerformed
private void ArchivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ArchivoActionPerformed
// TODO add your handling code here:
    ;
}//GEN-LAST:event_ArchivoActionPerformed
private void ImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ImprimirActionPerformed
    int sel2 = seleccion.getSelectedRow();
    if(sel2 != -1){
        String nombre = (String)seleccion.getValueAt(sel2, 0);
        Receta r = new Receta();
        r = buscarReceta(nombre);
        if(r != null)imprimir.impresionReceta(r);
        else System.out.println("Receta no encontrada.");
    }
    else System.out.println("Ninguna receta seleccionada.");

}//GEN-LAST:event_ImprimirActionPerformed
private void pasoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasoActionPerformed
int sel = seleccion.getSelectedRow();
    if(sel != -1){
        pasoApaso.setTitle("Pasos de: "+rec.getNombre());
        Paso.setText("1");
        String receta[] = new String[2];
        receta[0] = (String)seleccion.getValueAt(sel, 0);
        pasoApaso.setVisible(true);
        Receta r = buscarReceta(receta[0]);
        mostrarPasos(r.getId());
    }
    else{
        System.out.println("No hay seleccionada ninguna receta.");
    }
}//GEN-LAST:event_pasoActionPerformed
private void modificarLabels(Receta r){
    NombreLabel.setText(r.getNombre());
    DificultadLabel.setText(r.getDificultad());
    DuracionLabel.setText(Integer.toString(r.getDuracion())+"minutos");
    KiloLabel.setText(Double.toString(r.getKcal())+"KCals");
    TipoLabel.setText(r.getTipo().getNombre());
    NacionalidadLabel.setText(r.getNacionalidad().getNombre());
    VinoLabel.setText(r.getVino().getNombre());
}
private void seleccionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_seleccionMouseClicked
// TODO add your handling code here:
    //Cuando se clicka una tupla, se obtiene el nombre de la receta
    //y se busca entre las recetas diponibles, a fin de poder mostrar
    //su descripcion y fotografia
    int sel = seleccion.getSelectedRow();
    String receta = (String)seleccion.getValueAt(sel, 0);
    //informacion de control, descomentar para ver por consola
    //System.out.println(sel + " Hoho "+receta);
    //Ahora, teniendo la receta, toca buscarla en el array de recetas, 
    //obtener su descripcion, foto y lista de ingredientes.
    Receta r = new Receta();
    r = buscarReceta(receta);
    if(r != null){
        modificarLabels(r);
        rec = r;
        String fotoDesc = Integer.toString(r.getId());
        String descripcion1 = leerFichero(fotoDesc);
        /*descripcion.selectAll();
        descripcion.replaceSelection("");
        descripcion.append(descripcion1);*/
        cargarImagen(fotoDesc,foto);
    }
    else System.out.println("Receta no encontrada.");
}//GEN-LAST:event_seleccionMouseClicked

private void cargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cargarActionPerformed
// TODO add your handling code here:
    if(cargado == false){
        crearListaRecetas();
        DefaultTableModel modelo = new DefaultTableModel();
        modelo.addColumn("Nombre");
        modelo.addColumn("Dificultad");
        modelo.addColumn("Tipo");
        modelo.addColumn("Nacionalidad");
    
        Receta r = new Receta();
        int i = 0;
    
        while(i < recetas.size()){
            r = recetas.get(i);
            modelo.insertRow(i, new Object[]{recetas.elementAt(i).getNombre(),recetas.elementAt(i).getDificultad(),
                 recetas.elementAt(i).getTipo().getNombre(),recetas.elementAt(i).getNacionalidad().getNombre()});
            i++;
        }
        seleccion.setModel(modelo);
        cargado = true;
    }
    else System.out.println("Lista ya cargada.");
}//GEN-LAST:event_cargarActionPerformed

private void DetallesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DetallesActionPerformed
// TODO add your handling code here:
    
    if(rec != null){
        mostrarDetalles.setTitle("Detalles de la receta:: "+rec.getNombre());
        mostrarDetalles.setVisible(true);
        //Carga de las labels
        nombreDes.setText(rec.getNombre());
        nacionalidadDes2.setText(rec.getNacionalidad().getNombre());
        duracionDes2.setText(rec.getDuracion()+"minutos");
        tipoDes2.setText(rec.getTipo().getNombre());
        dificultadDes2.setText(rec.getDificultad());
        kcalDes2.setText(rec.getKcal()+"KCals.");
        //Carga del texto y la imagen
        String fotoDesc = Integer.toString(rec.getId());
        String descripcion1 = leerFichero(fotoDesc);
        descripcionDes.selectAll();
        descripcionDes.replaceSelection("");
        descripcionDes.append(descripcion1);
        cargarImagen(fotoDesc,fotoDes);
        //Carga de los ingredientes
        DefaultTableModel modelo = new DefaultTableModel();
        modelo.addColumn("Nombre");
        modelo.addColumn("Cantidad");
        int i = 0;
        while(i < rec.getIngredientes().size()){
            Vector<Ingrediente> ing = new Vector<Ingrediente>(0,1);
            ing = (Vector)rec.getIngredientes();
            modelo.insertRow(i, new Object[]{ing.elementAt(i).getProducto().getNombre(),
                ing.elementAt(i).getCantidad()+" "+ing.elementAt(i).getUnidad()});
            i++;
        }
        ingredientes.setModel(modelo);
    }
    else System.out.println("Seleccione primero una receta.");
}//GEN-LAST:event_DetallesActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    if(rec != null){
        Vector<Receta> aux = new Vector<Receta>(0,1);
        aux = cargarBD();
        Receta r2 = new Menu().CombinarReceta(rec, aux);
        mostrarReceta m = new mostrarReceta();
        Vector<Receta> vec = new Vector<Receta>(0,1);
        vec.add(r2);
        m.setRecetas(vec);
        m.setVisible(true);
    }
}//GEN-LAST:event_jButton1ActionPerformed

public void google(String stm){
    try{
        Process p = Runtime.getRuntime().exec("C:/Archivos de programa/Mozilla Firefox/firefox.exe "+stm);
    }catch(Exception e){
        e.printStackTrace();
    };
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    Receta r = new Receta();
    int i = seleccion.getSelectedRow();
    if(i != -1){
        String receta = (String)seleccion.getValueAt(i, 0);
        r = buscarReceta(receta);
        String d = r.getCalle()+"+"+r.getNacionalidad().getDireccion().getCiudad();
        if(r != null){
            String stm = "http://maps.google.es/maps?q="+d;
                    /*r.getCalle()+"+"+r.getNacionalidad().getDireccion().getCiudad()+"+"+
                r.getNacionalidad().getNombre();*/
            google(stm);
        }
    }
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
    int sel2 = seleccion.getSelectedRow();
    if(sel2 != -1){
        String nombre = (String)seleccion.getValueAt(sel2, 0);
        Receta r = new Receta();
        r = buscarReceta(nombre);
        if(r != null)new imprimir().impresionIngs(r);
        else System.out.println("Receta no encontrada.");
    }
    else System.out.println("Ninguna receta seleccionada.");
}//GEN-LAST:event_jButton3ActionPerformed
private void localizarActionPerformed(java.awt.event.ActionEvent evt){
    Receta r = new Receta();
    int i = seleccion.getSelectedRow();
    if(i != -1){
        String receta = (String)seleccion.getValueAt(i, 0);
        r = buscarReceta(receta);

        if(r != null){
            String stm = "http://maps.google.es/maps?q="+r.getNacionalidad().getDireccion().getCiudad()+"+"+
                r.getNacionalidad().getNombre();
            google(stm);
        }
    }
}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new mostrarReceta().setVisible(true);
            }
        });
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Anterior;
    private javax.swing.JMenu Archivo16;
    private javax.swing.JButton Detalles;
    private javax.swing.JLabel DificultadLabel;
    private javax.swing.JLabel DuracionLabel;
    private javax.swing.JLabel Imagen;
    private javax.swing.JMenuItem Imprimir16;
    private javax.swing.JLabel KiloLabel;
    private javax.swing.JMenuBar Menu16;
    private javax.swing.JLabel NacionalidadLabel;
    private javax.swing.JLabel NombreLabel;
    private javax.swing.JLabel Paso;
    private javax.swing.JTextArea Pasos;
    private javax.swing.JButton Siguiente;
    private javax.swing.JLabel TipoLabel;
    private javax.swing.JLabel VinoLabel;
    private javax.swing.JButton cargar;
    private javax.swing.JTextArea descripcionDes;
    private javax.swing.JScrollPane descripcionPaso;
    private javax.swing.JLabel dificultadDes;
    private javax.swing.JLabel dificultadDes2;
    private javax.swing.JLabel duracionDes;
    private javax.swing.JLabel duracionDes2;
    private javax.swing.JLabel foto;
    private javax.swing.JLabel fotoDes;
    private javax.swing.JTable ingredientes;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel kcalDes;
    private javax.swing.JLabel kcalDes2;
    private javax.swing.JButton localizar;
    private javax.swing.JFrame mostrarDetalles;
    private javax.swing.JLabel nacionalidadDes;
    private javax.swing.JLabel nacionalidadDes2;
    private javax.swing.JLabel nombreDes;
    private javax.swing.JButton paso;
    private javax.swing.JFrame pasoApaso;
    private javax.swing.JTable seleccion;
    private javax.swing.JLabel tipoDes;
    private javax.swing.JLabel tipoDes2;
    // End of variables declaration//GEN-END:variables

}
